<?php

namespace App\Services;


use GuzzleHttp\Client;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Session;

class OAuthService extends BaseService
{
	protected $connect_url;
	protected $client_id;
	protected $client_secret;
	protected $scope;
	protected $redirect_url;
	protected $client;
	protected $userinfo;
	protected $code_redirect_url;

	public function __construct()
	{
		$this->client = new Client();
	}

	public function get_access_token($service, $code)
	{
		$this->client_id = Config::get("oauth.{$service}.client_id");
		$this->client_secret = Config::get("oauth.{$service}.client_secret");
		$this->scope = Config::get("oauth.{$service}.scope");
		$this->redirect_url = Config::get("oauth.{$service}.redirect_url");
		$this->connect_url = Config::get("oauth.{$service}.connect_url");
		$access_token_url = Config::get("oauth.{$service}.access_url");
		if ($service == 'wx') {
			$access_token_url = $access_token_url . "?appid={$this->client_id}&secret={$this->client_secret}&code={$code}&grant_type=authorization_code";
		} elseif ($service == 'qq') {
			$this->code_redirect_url = Config::get("oauth.{$service}.code_redirect_url");
			$access_token_url = $access_token_url . "?client_id={$this->client_id}&client_secret={$this->client_secret}&code={$code}&grant_type=authorization_code&redirect_uri={$this->code_redirect_url}";
		}
		$result = $this->client->get($access_token_url);
		$result = $result->getBody()->getContents();

		return $result;
	}

	public function get_user_info($service, $code)
	{
		$data = $this->get_access_token($service, $code);
		if (empty($data)){
			return false;
		}
		if ($service == 'wx') {
			$data = json_decode($data, true);
		}elseif($service == 'qq'){
			$temp = explode('&', $data);
			$data = [];
			foreach($temp as $key => $value){
				$result = explode('=', $value);
				$data[$result[0]] = $result[1];
			}

		}
		$access_token = $data['access_token'];
		$openid = isset($data['openid']) ? $data['openid'] : $this->get_open_id($service, $data['access_token']);
		$this->userinfo = Config::get("oauth.{$service}.userinfo");
		$url = "{$this->userinfo}?access_token={$access_token}&openid={$openid}&oauth_consumer_key={$this->client_id}";
		$user_info = $this->client->get($url);
		$user_info = $user_info->getBody()->getContents();
		$user_info = json_decode($user_info, true);
		if ($service == 'wx' && isset($user_info['errcode'])) {
			return false;
		}elseif($service == 'qq' && !isset($user_info['nickname'])){
			return false;
		}

		return $user_info;
	}

	public function get_open_id($service, $access_token)
	{
		$this->userinfo = Config::get("oauth.{$service}.get_openid_url");
		$url = "{$this->userinfo}?access_token={$access_token}";
		$openid = $this->client->get($url);
		$openid = $openid->getBody()->getContents();
		if($service == 'qq'){
			//----检测错误是否发生
			if(strpos($openid, "callback") !== false)
			{

				$lpos = strpos($openid, "(");
				$rpos = strrpos($openid, ")");
				$openid = substr($openid, $lpos + 1, $rpos - $lpos -1);
			}else {
				return false;
			}
		}
		$openid = json_decode($openid, true);
		if (isset($openid['msg']) || isset($openid['error'])) {
			return false;
		}
		Session::put('qq_open_id', $openid['openid']);

		return $openid['openid'];
	}
}